﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace System.Security.Cryptography
{
    /// <summary>
    /// DES加密解密帮助类
    /// </summary>
    public static class DESHelper
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="input">要加密的字符串</param>
        /// <param name="encrKey">加密密钥</param>
        /// <returns></returns>
        public static string Encrypt(string input, string encrKey)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            Byte[] byKey = { };
            Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            byKey = Encoding.UTF8.GetBytes(encrKey);
            var des = new DESCryptoServiceProvider();
            Byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
            using (var ms = new MemoryStream())
            {
                var cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="input">要解密的字符串</param>
        /// <param name="decryptKey">密钥</param>
        /// <returns></returns>
        public static string Decrypt(string input, string decryptKey)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            Byte[] byKey = { };
            Byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            Encoding encoding = Encoding.UTF8;
            byKey = encoding.GetBytes(decryptKey.Substring(0, 8));
            var des = new DESCryptoServiceProvider();
            var inputByteArray = Convert.FromBase64String(input);
            using (var ms = new MemoryStream())
            {
                var cs = new CryptoStream(ms, des.CreateDecryptor(byKey, iv), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                des.Clear();
                return encoding.GetString(ms.ToArray(), 0, ms.ToArray().Length);
            }
        }
        public static string GetFileMD5(this string filePath)
        {
            using (Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                var md5Provider = new MD5CryptoServiceProvider();
                byte[] bytes = md5Provider.ComputeHash(stream);
                var sBuilder = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    sBuilder.Append(bytes[i].ToString("X2"));
                }
                return sBuilder.ToString();
            }
        }
    }
}